package com.wm.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bean.Person;

import com.wm.bean.Restaurant;
import com.wm.dao.RestaurantDao;

@Transactional
public class RestaurantDaoHibernate implements RestaurantDao {
	@Resource
	private SessionFactory sessionFactory;
	private String hql;
	
	public void delete(Integer restaurantid) {
		// TODO Auto-generated method stub

	}

	public Person getPerson(Integer restaurantid) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Restaurant> getRestaurants(int beginNum, int maxResults,String uid) {
		// sessionFactory.getCurrentSession().createQuery("from
		// Restaurant").list();
		String hql = "from Restaurant r";
		if (!"".equals(uid)) {
			hql += " where r.uid = '" + uid + "'";
		}
		System.out.println(hql);
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Restaurant> rList = query.list();
		System.out.println(rList.get(0).getName());
		return rList;

	}
	
/*	public List<Restaurant> getRestaurantsByName(int beginNum, int maxResults, List<String> names) {
		
//		ArrayList<String> keyWord = (ArrayList<String>)names;
		
		String conditionString ="";
		for (int i = 0; i < names.size(); i++) {
			conditionString +=  " or Restaurant.name like '%" + names.get(i) + "%' "; 
		}
//		hql = "from Restaurant where 1=1 " + conditionString;
		Query query = sessionFactory.getCurrentSession().createQuery(
				"from Restaurant where 1=1 " + conditionString);
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Restaurant> rList = query.list();
		System.out.println(rList.get(0).getName());
		return rList;

	}*/

public List<Restaurant> getRestaurantsByNameHql(int beginNum, int maxResults, String hql) {
		
//		ArrayList<String> keyWord = (ArrayList<String>)names;
		
		
//		hql = "from Restaurant where 1=1 " + conditionString;
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setFirstResult(beginNum);
		query.setMaxResults(maxResults);
		List<Restaurant> rList = query.list();
//		System.out.println(rList.get(0).getName());
		return rList;

	}
	@Transactional
	public void save(Restaurant restaurant) {
		sessionFactory.getCurrentSession().persist(restaurant);

	}

	public void update(Restaurant restaurant) {
		// TODO Auto-generated method stub

	}

	public int getRestPages(int maxResults, String uid) {
		int count = 0;
		String hql = "from Restaurant r";
		if (!"".equals(uid)) {
			hql += " where r.uid = '" + uid + "'";
		}
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}
	
	public int getRestPagesWithHql(int maxResults, String hql) {
		int count = 0;

		Query query = sessionFactory.getCurrentSession().createQuery(hql);
//		count = ((Number) query.iterate().next())
//        .intValue();
		count = query.list().size();
		double countPage = count / (maxResults * 1.0);

		int countpage = (int) Math.ceil(countPage);
		System.out.println("总共有" + count + "条记录每页显示" + maxResults + "条总共"
				+ countpage + "页");
		return countpage;
	}

	



}
